import pandas as pd#读取数据库文件
import numpy as np
from sklearn.model_selection import train_test_split#划分数据集
from sklearn.tree import DecisionTreeClassifier#决策树
from sklearn.metrics import classification_report#分类报告

#1.读取数据
data = pd.read_csv("data.csv",header=0)
x = data.drop(columns='Label')
y = data['Label']


#2.特征处理
C=['GAP_between_Create_Last','TOTAL_EXPENSED_AMOUNT','APPROVED_AMT','PAID_IN_AMOUNT_ALLOCATION','EXPENSE_TYPE_DESC','CITY']
UnChooseC=[0,0,0,0,0,0]     #选择忽略那些特征
for i in range(6):
    if(UnChooseC[i]==1):
        x.drop(columns=C[i],inplace=True)

#3.划分数据集
x_train, x_test, y_train, y_test = train_test_split(x, y)

#4.模型训练
for i in range(2,20):     #设置树的最大深度     
    print("max_depth:",i)
    clf=DecisionTreeClassifier(criterion='entropy',max_depth=i) #基于信息熵
    clf.fit(x_train,y_train)
    
#5.测试
    predict=clf.predict(x_test)

#6.输出结果及分析
    np.set_printoptions(precision=4)
    importances=clf.feature_importances_
    print("The importance of features: ",importances)
    print(classification_report(y_test,predict))
